package com.example.fragment_demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button1,button2;
    private Fragment fr_right,fr_left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        button1 = findViewById(R.id.f_right);
        button2= findViewById(R.id.f_left);
        fr_right=new fr_right();
        fr_left=new fr_left();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建碎片管理器
                FragmentManager  fm = getSupportFragmentManager();
                //创建事务
                FragmentTransaction transaction = fm.beginTransaction();
                //调用碎片，替换主页的fragment
                transaction.replace(R.id.fr_content,fr_right);
                //进行替换
                transaction.commit();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建碎片管理器
                FragmentManager   fm = getSupportFragmentManager();
                //创建事务
                FragmentTransaction transaction = fm.beginTransaction();
                //调用碎片，替换主页的fragment
                transaction.replace(R.id.fr_content,fr_left);
                //进行替换
                transaction.commit();
            }
        });
    }
}
